﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Xml;

namespace Mirkuz.Mapping
{
    public class MapTrack
    {
        private List<MapPoint> _list = new List<MapPoint>();
        public MapPoint StartPoint;
        public MapPoint FinishPoint;
        public MapPoint LastPoint;
        public string Name;
        public string Description;

        public List<MapPoint> TrackPoints
        {
            get { return _list; }
        }

        public Pen Pen = new Pen(Color.Red, 2);

        public void AddStartPoint(MapPoint start)
        {
            StartPoint = start;
            _list.Add(start);
            LastPoint = StartPoint;
        }

        public void AddFinishPoint(MapPoint finish)
        {
            FinishPoint = finish;
            _list.Add(finish);
            LastPoint = FinishPoint;
        }

        public void AddPoint(MapPoint p)
        {
            _list.Add(p);
            LastPoint = p;
        }

        public void StreamToKML(XmlWriter xtr)
        {
            xtr.WriteStartElement("Placemark");
            {
                xtr.WriteStartElement("Name");
                xtr.WriteString(Name);
                xtr.WriteEndElement();

                xtr.WriteStartElement("Description");
                xtr.WriteString(Description);
                xtr.WriteEndElement();

                //Style region
                xtr.WriteStartElement("Style");
                {
                    xtr.WriteStartElement("LineStyle");
                    {
                        xtr.WriteStartElement("color");
                        xtr.WriteString("ffff00ff");
                        xtr.WriteEndElement();

                        xtr.WriteStartElement("width");
                        xtr.WriteString("4");
                        xtr.WriteEndElement();
                    }
                    xtr.WriteEndElement();
                }
                xtr.WriteEndElement();

                //Line Coords
                xtr.WriteStartElement("LineString");
                {
                    xtr.WriteStartElement("coordinates");
                    xtr.WriteString(GetCoordinateString());
                    xtr.WriteEndElement();
                }
                xtr.WriteEndElement();
            }
            xtr.WriteEndElement();
        }

        private String GetCoordinateString()
        {
            StringBuilder sb = new StringBuilder();

            for (int x = 0; x < _list.Count; x++)
            {
                sb.Append(
                  _list[x].Longitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + "," +
                  _list[x].Latitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + "," +
                  _list[x].Altitude.ToString(System.Globalization.CultureInfo.InvariantCulture.NumberFormat) + " ");
            }

            return sb.ToString();

        }
    }
}
